package com.jeehentor.system.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeehentor.common.vo.Result;
import com.jeehentor.dto.RoleDTO;
import com.jeehentor.dto.SaveRoleDTO;
import com.jeehentor.entity.SysRole;
import com.jeehentor.system.service.ISysRoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author wzd
 * @since 2024-04-08
 */
@RestController
@RequestMapping("/role")
@RequiredArgsConstructor
@Tag(name = "角色管理")
public class SysRoleController {
    private final ISysRoleService roleService;


    @Operation(summary = "角色列表")
    @PostMapping("/pageList")
    public Result<Page<SysRole>> pageList(@RequestBody RoleDTO roleDTO){
        LambdaQueryWrapper<SysRole> roleModelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roleModelLambdaQueryWrapper.like(StringUtils.isNotBlank(roleDTO.getRoleName()),SysRole::getRoleName, roleDTO.getRoleName());
        Page<SysRole> page = roleService.page(roleDTO.getPage(), roleModelLambdaQueryWrapper);
        return Result.OK(page);
    }

    @Operation(summary = "角色列表(无分页)")
    @PostMapping("/list")
    public Result<List<SysRole>> list(@RequestBody RoleDTO roleDTO){
        LambdaQueryWrapper<SysRole> roleModelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roleModelLambdaQueryWrapper.like(StringUtils.isNotBlank(roleDTO.getRoleName()),SysRole::getRoleName, roleDTO.getRoleName());
        if(StringUtils.isNotBlank(roleDTO.getRoleCodes())){
            String[] split = roleDTO.getRoleCodes().split(",");
            roleModelLambdaQueryWrapper.in(SysRole::getRoleCode, Arrays.asList(split) );
        }
        List<SysRole> list = roleService.list(roleDTO.getPage(), roleModelLambdaQueryWrapper);
        return Result.OK(list);
    }

    @Operation(summary = "新建角色")
    @PostMapping("/saveRole")
    public Result<String> saveRole(@RequestBody SaveRoleDTO roleModel){
        roleService.saveRole(roleModel);
        return Result.ok("保存成功");
    }

    @Operation(summary = "更新角色")
    @PostMapping("/updateRole")
    public Result<String> updateRole(@RequestBody SaveRoleDTO roleModel){
        roleService.updateRole(roleModel);
        return Result.ok("修改成功");
    }

    @Operation(summary = "删除角色")
    @PostMapping("/deleteRole")
    public Result<String> deleteRole(@RequestBody SaveRoleDTO roleModel){
        roleService.deleteRole(roleModel);
        return Result.ok("删除成功");
    }


    @Operation(summary = "角色明细")
    @PostMapping("/roleDetail")
    public Result<SaveRoleDTO> roleDetail(@RequestBody SaveRoleDTO roleModel){
        SaveRoleDTO roleDetail = roleService.roleDetail(roleModel);
        return Result.OK(roleDetail);
    }
}
